<?php

namespace App\Model\Stat;

use App\Helpers\OpeninstallUtil;
use Hyperf\DbConnection\Model\Model;

/**
 *  每日统计-openinstall
 */
class StatOpeninstall extends Model
{
    protected $connection = 'stat';
    protected $table = 'stat_daily_openinstall_channel';

    static function updateRecord($start = '', $end = '', $channel = '', $packName = '')
    {
        $where = ['dt' => $start];
        if ($channel) $where['channel'] = $channel;
        $model = self::where($where)->first();
        if (!$model) $model = new self();
        $model->dt = $start;
        $model->channel = $channel;
        $channel == 'all' && $channel = '';
        $data = OpeninstallUtil::getGrowth($start, $end, $channel, $packName, false);
        log_info(__METHOD__ . "=={$start}=={$channel}==Data:" . json_encode($data));
        if (!$data['body']) return false;
        $data = $data['body'][0];
        $model->install = $data['install'];
        $model->register = $data['register'];
        $model->survive_d1 = $data['survive_d1'];
        $model->survive_d7 = $data['survive_d7'];
        $model->survive_d30 = $data['survive_d30'];
        $data = OpeninstallUtil::getGrowth($start, $end, $channel, $packName, true);
        log_info(__METHOD__ . "=={$start}=={$channel}==ExDupData:" . json_encode($data));
        $data = $data['body'][0];
        $model->install_exdup = $data['install'];
        return $model->saveOrFail();
    }
}
